package com.akupilih.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.akupilih.app.adapter.StatistikAdapter;
import com.akupilih.app.helper.PagerSlidingTabStrip;

public class StatistikFragment extends Fragment {
	private ViewPager mViewPager;
	private FragmentActivity myContext;
	private PagerSlidingTabStrip tabs;

	public StatistikFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_statistik,
				container, false);

		return rootView;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		myContext = (FragmentActivity) activity;

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		setHasOptionsMenu(true);
		FragmentChart();
	}

	protected void FragmentChart() {
		StatistikAdapter titleAdapter = new StatistikAdapter(
				myContext.getSupportFragmentManager());

		/** Defining a listener for pageChange */
		ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				super.onPageSelected(position);
			}
		};

		mViewPager = (ViewPager) myContext.findViewById(R.id.viewpager);
		tabs = (PagerSlidingTabStrip) myContext.findViewById(R.id.tabs);
		mViewPager.setAdapter(titleAdapter);
		mViewPager.setCurrentItem(0);
		mViewPager.setOnPageChangeListener(pageChangeListener);
		tabs.setViewPager(mViewPager);
		tabs.setOnPageChangeListener(pageChangeListener);
		tabs.setIndicatorColor(Color.parseColor("#CC0000"));
		tabs.setIndicatorHeight(3);

	}

	@Override
	public void onPrepareOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onPrepareOptionsMenu(menu);
		menu.findItem(R.id.voice).setVisible(false);
		
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub

		inflater.inflate(R.menu.chart, menu);
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.helpchart:
			popUpHelp();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void popUpHelp() {
		AlertDialog.Builder dialog = new AlertDialog.Builder(myContext);
		LayoutInflater inflate = myContext.getLayoutInflater();
		dialog.setView(inflate.inflate(R.layout.dialog_helpchart, null))
				.setNegativeButton("Close",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								dialog.cancel();

							}
						});
		dialog.setCancelable(true);
		dialog.setCustomTitle(inflate
				.inflate(R.layout.dialog_customtitle, null));
		dialog.show();

	}
}
